﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Translat-Sql
            var sql = string.Format("select * from Users");
            var dt1 = DbHelper.GetDataTable(sql);
            dataGridView1.DataSource = dt1;

            var sqlArt = string.Format("select * from Articles");
            var dt2 = DbHelper.GetDataTable(sqlArt);
            dataGridView2.DataSource = dt2;

            dataGridView1.ReadOnly = true;
            dataGridView2.ReadOnly = true;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView2.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dataGridView1.AllowUserToAddRows = false;
            dataGridView2.AllowUserToAddRows = false;
        }

        //Users表查找
        private void button1_Click(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            var sql = string.Format("select * from Users where Username like'%{0}%'", name);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView1.DataSource = dt;

        }

        //Articles表查询
        private void button5_Click(object sender, EventArgs e)
        {
            var name = textBox2.Text;

            var sql = string.Format("select * from Articles where Author like '%{0}%'", name);

            var dt = DbHelper.GetDataTable(sql);

            dataGridView2.DataSource = dt;
        }

        //Users表删除
        private void button2_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var row = dataGridView1.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Users where Id = {0}", id);

                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (rowCount > 0)
                    {
                        var sqlUse = "select * from Users";

                        var dt = DbHelper.GetDataTable(sqlUse);

                        dataGridView1.DataSource = dt;

                    }

                }

            }
            else
            {
                MessageBox.Show("未选择要删除的数据", "提示");
            }
        }

        //Articles表删除
        private void button6_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                var row = dataGridView2.SelectedRows[0];

                var id = (int)row.Cells["Id"].Value;

                var res = MessageBox.Show("是否删除", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

                if (res.Equals(DialogResult.Yes))
                {
                    var sql = string.Format("delete from Articles where Id ={0}", id);

                    var rowCount = DbHelper.AddOrEditOrDeleteSave(sql);

                    if (rowCount > 0)
                    {
                        var sqlUse = "select * from Articles";

                        var dt = DbHelper.GetDataTable(sqlUse);

                        dataGridView2.DataSource = dt;

                    }

                }

            }
            else
            {
                MessageBox.Show("未选择要删除的数据", "提示");
            }
        }

        //Users表添加
        private void button4_Click(object sender, EventArgs e)
        {
            Editform editform = new Editform();
            var res = editform.ShowDialog();

            if (res == DialogResult.Yes)
            {
                var sqlUse = "select * from Users";

                var dt = DbHelper.GetDataTable(sqlUse);

                dataGridView1.DataSource = dt;
            }
        }

        //Users表更新
        private void button3_Click(object sender, EventArgs e)
        {
            if (dataGridView1.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView1.SelectedRows[0].Cells["Id"].Value;
                var name = (string)dataGridView1.SelectedRows[0].Cells["Username"].Value;
                var pwd = (string)dataGridView1.SelectedRows[0].Cells["Password"].Value;

                Editform editform = new Editform(id,name,pwd);
                var res = editform.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var sqlUse = "select * from Users";

                    var dt = DbHelper.GetDataTable(sqlUse);

                    dataGridView1.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("未选择更新的行","提示");
            }

        }

        //Articles表更新
        private void button7_Click(object sender, EventArgs e)
        {
            if (dataGridView2.SelectedRows.Count > 0)
            {
                var id = (int)dataGridView2.SelectedRows[0].Cells["Id"].Value;
                var title = (string)dataGridView2.SelectedRows[0].Cells["Title"].Value;
                var author = (string)dataGridView2.SelectedRows[0].Cells["Author"].Value;
                var createdTime = (string)dataGridView2.SelectedRows[0].Cells["CreatedTime"].Value;

                Editform1 editform1 = new Editform1(id, title, author, createdTime);
                var res = editform1.ShowDialog();
                if (res == DialogResult.Yes)
                {
                    var sqlUse = "select * from Articles";

                    var dt = DbHelper.GetDataTable(sqlUse);

                    dataGridView2.DataSource = dt;
                }
            }
            else
            {
                MessageBox.Show("未选择更新的行", "提示");
            }
        }

        //Articles表添加
        private void button8_Click(object sender, EventArgs e)
        {
            Editform1 editform1 = new Editform1();
            var res = editform1.ShowDialog();
            if (res == DialogResult.Yes)
            {
                var sqlUse = "select * from Articles";

                var dt = DbHelper.GetDataTable(sqlUse);

                dataGridView2.DataSource = dt;
            }
        }
    }
}
